﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace SourceBackup
{
    public partial class MaintainFilters : Form
    {
        public MaintainFilters()
        {
            InitializeComponent();
        }

        #region Form events
        private void MaintainFilters_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.BackupFilters != null)
            {
                foreach (string backupFilterSerialized in Properties.Settings.Default.BackupFilters)
                {
                    BackupFilter backupFilter = SerializationUtils.DeserializeXML <BackupFilter>(backupFilterSerialized);
                    ListViewItem lvi = new ListViewItem(backupFilter.Name);
                    lvi.SubItems.Add(backupFilter.Filters());
                    lvi.Tag = backupFilter;
                    lvwFilters.Items.Add(lvi);
                }
            }
            DelayExecute.Execute(this, 200, (MethodInvoker)ResizeListViewColumns);
        } 
        #endregion

        #region Listview events
        private void lvwFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonEdit.Enabled = lvwFilters.SelectedItems.Count == 1;
            toolStripButtonDelete.Enabled = lvwFilters.SelectedItems.Count > 0 && lvwFilters.Items.Count > lvwFilters.SelectedItems.Count;
            toolStripButtonExport.Enabled = lvwFilters.SelectedItems.Count > 0;
        }
        private void lvwFilters_DoubleClick(object sender, EventArgs e)
        {
            toolStripButtonEdit_Click(sender, e);
        }
        private void lvwFilters_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonEdit_Click(sender, e);
            }
        }
        private void lvwFilters_Resize(object sender, EventArgs e)
        {
            DelayExecute.Execute(this, 200, (MethodInvoker)ResizeListViewColumns);
        }
        private void ResizeListViewColumns()
        {
            columnHeaderFilter.Width = lvwFilters.ClientSize.Width - columnHeaderName.Width - 1; 
        }
        #endregion

        #region Toolbar button events
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            using (EditFilter editFilter = new EditFilter())
            {
                if (editFilter.ShowDialog() == DialogResult.OK)
                {
                    ListViewItem lvi = new ListViewItem(editFilter.SelectedFilter.Name);
                    lvi.SubItems.Add(editFilter.SelectedFilter.Filters());
                    lvi.Tag = editFilter.SelectedFilter;
                    lvwFilters.Items.Add(lvi);
                    SaveFilters();
                }
            }
        }
        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (lvwFilters.SelectedItems.Count == 1)
            {
                using (EditFilter editFilter = new EditFilter())
                {
                    editFilter.SelectedFilter = (BackupFilter)lvwFilters.SelectedItems[0].Tag;
                    if (editFilter.ShowDialog() == DialogResult.OK)
                    {
                        lvwFilters.SelectedItems[0].Text = editFilter.SelectedFilter.Name;
                        lvwFilters.SelectedItems[0].SubItems[1].Text = editFilter.SelectedFilter.Filters();
                        lvwFilters.SelectedItems[0].Tag = editFilter.SelectedFilter;
                        SaveFilters();
                    }
                }
            }
        }
        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (lvwFilters.SelectedItems.Count > 0 && lvwFilters.Items.Count > lvwFilters.SelectedItems.Count)
            {
                if (MessageBox.Show("Are you sure you want to remove the selected filter(s)?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (ListViewItem lvi in lvwFilters.SelectedItems)
                    {
                        lvwFilters.Items.Remove(lvi);
                    }
                    SaveFilters();
                }
            }
        } 
        private void toolStripButtonImport_Click(object sender, EventArgs e)
        {
            if (openFileDialogImport.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    string serializedContent;
                    ListViewItem lviImport = null;
                    serializedContent = File.ReadAllText(openFileDialogImport.FileName);
                    BackupFilter backupFilter = SerializationUtils.DeserializeXML<BackupFilter>(serializedContent);
                    foreach (ListViewItem lvi in lvwFilters.Items)
                    {
                        if (lvi.Text == backupFilter.Name)
                        {
                            lviImport = lvi;
                        }
                    }
                    if (lviImport != null)
                    {
                        if (MessageBox.Show("There is an existing filter with the same name! Should it be replaced?", "Replace", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                            lviImport = null;
                    }
                    if (lviImport == null)
                    {
                        lviImport = new ListViewItem(backupFilter.Name);
                        lviImport.SubItems.Add(backupFilter.Filters());
                        lviImport.Tag = backupFilter;
                        lvwFilters.Items.Add(lviImport);
                    }
                    else
                    {
                        lviImport.SubItems[1].Text = backupFilter.Filters();
                        lviImport.Tag = backupFilter;
                    }
                    SaveFilters();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }     
        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvwFilters.SelectedItems)
            {
                BackupFilter backupFilter = (BackupFilter)lvi.Tag;
                saveFileDialogExportFilter.Title = "Export filter: " + backupFilter.Name;
                saveFileDialogExportFilter.FileName = backupFilter.Name;
                if (saveFileDialogExportFilter.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(saveFileDialogExportFilter.FileName, SerializationUtils.SerializeToXML <BackupFilter>(backupFilter));
                }
            }
        }
        #endregion

        #region Private methods
        private void SaveFilters()
        {
            try
            {
                List<string> tmpList = new List<string>();
                Properties.Settings.Default.BackupFilters = new System.Collections.Specialized.StringCollection();
                foreach (ListViewItem lvi in lvwFilters.Items)
                {
                    tmpList.Add(SerializationUtils.SerializeToXML<BackupFilter>(((BackupFilter)lvi.Tag)));
                    
                }
                tmpList.Sort();
                foreach (string filter in tmpList)
                {
                    Properties.Settings.Default.BackupFilters.Add(filter);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        } 
        #endregion
        
    }
}
